package ch.bfh.cmdh.thinkerforge.weatherstation.view.display;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import ch.bfh.cmdh.thinkerforge.weatherstation.controller.WeatherStationController;
import ch.bfh.cmdh.thinkerforge.weatherstation.model.WeatherStationSensor;
import ch.bfh.cmdh.thinkerforge.weatherstation.model.ambientlight.AmbientLightSensor;
import ch.bfh.cmdh.thinkerforge.weatherstation.model.humidity.HumiditySensor;
import ch.bfh.cmdh.thinkerforge.weatherstation.model.temperature.TemperatureSensor;

import com.tinkerforge.AlreadyConnectedException;
import com.tinkerforge.BrickletLCD20x4;
import com.tinkerforge.IPConnection;
import com.tinkerforge.NotConnectedException;
import com.tinkerforge.TimeoutException;

public class DisplayBricklet implements Observer, WeatherStationSensor {

    private static final String DISPLAY_UID = "d19";
    private static final String FIVE_SPACES = "     ";
    private static final short FIRST_POSITION = 0;
    private static final short TWELFTH_POSITION = 11;
    private static final short FIRST_LINE = 0;
    private static final short SECOND_LINE = 1;
    private static final short THIRD_LINE = 2;
    private static final short FOURTH_LINE = 3;

    private String title = "Super Hyper Mega WS";
    private BrickletLCD20x4 lcdBricklet;
    private IPConnection ipConnection;

    public DisplayBricklet(WeatherStationController controller) {
        ipConnection = new IPConnection();
        lcdBricklet = new BrickletLCD20x4(DISPLAY_UID, ipConnection);
        try {
            ipConnection.connect(WeatherStationController.HOST, WeatherStationController.PORT);
            lcdBricklet.backlightOn();
            lcdBricklet.writeLine(FIRST_LINE, FIRST_POSITION, title);
        } catch (UnknownHostException e) {
            System.out.println("LCD UnknownHostException");
        } catch (AlreadyConnectedException e) {
            System.out.println("LCD AlreadyConnectedException");
        } catch (IOException e) {
            System.out.println("LCD IOException");
        } catch (TimeoutException e) {
            System.out.println("LCD TimeoutException");
        } catch (NotConnectedException e) {
            System.out.println("LCD NotConnectedException");
        }
    }

    @Override
    public void update(Observable o, Object arg) {

        HashMap<String, Integer> map = (HashMap<String, Integer>) arg;
        if (map.containsKey(AmbientLightSensor.AMBIENT_LIGHT_KEY)) {
            String text = "Ambient: " + map.get(AmbientLightSensor.AMBIENT_LIGHT_KEY) + " Lux";
            try {
                lcdBricklet.writeLine(SECOND_LINE, TWELFTH_POSITION, FIVE_SPACES);
                lcdBricklet.writeLine(SECOND_LINE, FIRST_POSITION, text);
            } catch (TimeoutException e) {
                System.out.println("LCD TimeoutException");
            } catch (NotConnectedException e) {
                System.out.println("LCD NotConnectedException");
            }
        }
        if (map.containsKey(TemperatureSensor.TEMPERATURE_KEY)) {
            String v = "?";
            try {
                v = String.valueOf(map.get(TemperatureSensor.TEMPERATURE_KEY));
                v = v.substring(0, 2) + "." + v.substring(2, 4);
            } catch (Exception e) {
                v = String.valueOf(TemperatureSensor.TEMPERATURE_KEY);
            }
            String text = "Temperature: " + (v) + " C";
            try {
                lcdBricklet.writeLine(THIRD_LINE, FIRST_POSITION, text);
            } catch (TimeoutException e) {
                System.out.println("LCD TimeoutException");
            } catch (NotConnectedException e) {
                System.out.println("LCD NotConnectedException");
            }
        }
        if (map.containsKey(HumiditySensor.HUMIDITY_KEY)) {
            String v = "?";
            try {
                v = String.valueOf(map.get(HumiditySensor.HUMIDITY_KEY));
                v = v.substring(0, 2) + "." + v.substring(2, 3);
            } catch (Exception e) {
                v = String.valueOf(map.get(HumiditySensor.HUMIDITY_KEY));
            }
            String text = "Humidity: " + v + " %";

            try {
                lcdBricklet.writeLine(FOURTH_LINE, FIRST_POSITION, text);
            } catch (TimeoutException e) {
                System.out.println("LCD TimeoutException");
            } catch (NotConnectedException e) {
                System.out.println("LCD NotConnectedException");
            }
        }
    }

    @Override
    public void connect() throws UnknownHostException, AlreadyConnectedException, IOException, TimeoutException,
            NotConnectedException {

    }

    @Override
    public void disconnect() throws NotConnectedException {
        try {
            lcdBricklet.clearDisplay();
            lcdBricklet.backlightOff();
        } catch (TimeoutException e) {
            System.out.println("LCD TimeoutException");
        }
        ipConnection.disconnect();
    }

}
